home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / modlib_s.lha / modlib_src / $deb.P < prev    next >
Text File  |  1990-04-12  |  12KB  |  354 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $deb.P */
  25.  
  26. /* Trace and Debug package */
  27. /* To be able to trace execution, you should either consult the
  28. file containing the predicate definitions or compile them with the 't'
  29. option and load them. The 't' option keeps the assembler from optimizing
  30. subroutine linkage and allows the trace to intercept any call. To trace
  31. the system, all predicates to be traced must be passed to trace/1. For 
  32. example,
  33.     :- trace([pred1/1,pred2/2]),trace(pred3/2).
  34. will set up tracing of pred1, pred2 and pred3 (of the indicated arities.)
  35. The compile and consult predicates have been modified to
  36. return a list of predicates defined, which can be directly passed to trace,
  37. to set up interception. This also turns tracing on. The facilities are 
  38. roughly  similar to CProlog's debugging facility, but you can only trace 
  39. routines for which you've done a trace/1. This makes trace and spy very 
  40. similar; it is essentially just two levels of spy points. The times that 
  41. tracing (and spying) occur differ somewhat from CProlog's. Here tracing 
  42. occurs at Call (i.e., entry to a predicate), successful Exit from a clause, 
  43. and Failure of an entire call. Skip, leap, etc. work in ways similar to
  44. CProlog. We do not support the leashing modes of CProlog (but it would be 
  45. easy enough to add).
  46. */
  47.  
  48. $deb_export([$debug/0,$nodebug/0,$trace/1,$untrace/1,$spy/1,$nospy/1,
  49.     $trace/0,$untrace/0, $debugging/0, $deb_tracepreds/1, $deb_spypreds/1]).
  50.  
  51. /* $deb_use($glob,[$globalset/1,$gennum/1,$gensym/2]).
  52.    $deb_use($call,[call/1,'_$interp'/2,'_$call'/1]).
  53.    $deb_use($meta,[$functor/3,$univ/2,$length/2]).
  54.    $deb_use($name,[$name/2,$name0/2]).
  55.    $deb_use($bio,[$writename/1,$writeqname/1,$put/1,$nl/0,$tab/1,
  56.     $tell/1,$tell/2,$telling/1,$told/0,$get/1,$get0/1,$see/1,$seeing/1,
  57.     $seen/0]).
  58.    $deb_use($io,[$write/1,$writeq/1,$display/1,$print/1]).
  59.    $deb_use($assert,[$assert/1,$asserti/2,$assert_union/2,$assert_call_s/1,
  60.         $assert_get_prref/2,$assert_put_prref/2,$assert_abolish_i/1]).
  61.    $deb_use($retr,[$retract/1,_,_]).
  62.    $deb_use($defint,[$defint_call/4]).
  63.    $deb_use($buff,[$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,
  64.         $symtype/2,
  65.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  66.         $pred_undefined/1, $hashval/3]).
  67. */
  68.  
  69. $debug :- 
  70.     $globalset($deb_ugging(1)),
  71.     $globalset($deb_tracing(1)),
  72.     $globalset($deb_invoke_num(1)),
  73.     $globalset($deb_skipping(0)),
  74.     $globalset($deb_quasiskip(0)).
  75.  
  76. $nodebug :- 
  77.     $globalset($deb_ugging(0)),
  78.     $globalset($deb_skipping(1)),
  79.     $globalset($deb_tracing(0)).
  80.  
  81. $deb_trace(Call) :-
  82.     $deb_ugging(D),
  83.     (D=:=0,'_$call'(Call);
  84.      D=\=0,
  85.        $deb_invoke_num(N),N1 is N+1,$globalset($deb_invoke_num(N1)),
  86.        $deb_tracing(T),
  87.        (T=:=0,'_$call'(Call);
  88.         T=\=0, /* $deb_tracing */
  89.            $deb_quasiskip(S),    /* skipping implies quasiskipping */
  90.            (S=\=0,'_$call'(Call) /* skipping or quasi_skipping */
  91.            ;
  92.             S=:=0,$deb_enterpred(N,Call,nospy)
  93.                 )
  94.        )
  95.     ).
  96.  
  97. /* routine for processing spy points */
  98.  
  99. $deb_spy(Call) :-
  100.     $deb_ugging(D),
  101.     (D=:=0,'_$call'(Call)
  102.     ;
  103.      D=\=0,
  104.         $deb_invoke_num(N),N1 is N+1,$globalset($deb_invoke_num(N1)),
  105.         $deb_skipping(S),
  106.         (S=\=0,'_$call'(Call) /* skipping */
  107.         ;
  108.          S=:=0,
  109.         $globalset($deb_tracing(1)),
  110.         $deb_enterpred(N,Call,spy)
  111.              )
  112.     ).
  113.  
  114. $deb_enterpred(N,Call,Spy) :-
  115.     $deb_traceget(N,Spy,'Call',Call,_),
  116.     $deb_tracing(D1),
  117.     (D1=:=0,'_$call'(Call)
  118.     ;
  119.      D1=\=0, /* $deb_tracing */
  120.        ('_$call'(Call),
  121.         $deb_tracing(D2),
  122.         (D2=:=0;
  123.          D2=\=0,
  124.          $globalset($deb_quasiskip(0)),$globalset($deb_skipping(0)),
  125.          $deb_traceget(N,Spy,'Exit',Call,_)
  126.         )
  127.        ;
  128.         $globalset($deb_quasiskip(0)),$globalset($deb_skipping(0)),
  129.         $deb_tracing(D3),
  130.         D3=\=0, /*fail if not still tracing */
  131.         $deb_traceget(N,Spy,'Fail',Call,C2),
  132.         C2=114, /* r: retry, otherwise just fail */
  133.         $globalset($deb_invoke_num(N)), /* reset call number */
  134.         $deb_retry(Call,Spy)
  135.        )
  136.     ).
  137.  
  138. $deb_retry(Call,spy) :- !,$deb_spy(Call).
  139. $deb_retry(Call,nospy) :- $deb_trace(Call).
  140.  
  141. $deb_traceget(N,Spy,Type,Call,C) :-
  142.         $telling(Of),$tell(user),
  143.         $deb_writepref(Spy),
  144.         $writename(' ('),$writename(N),$writename(') '),
  145.         $writename(Type),$writename(': '),
  146.         Call =.. [Clpred|Args],
  147.         $name(Clpred,[99,111,100,101,36|Namelist]), /* code$ */
  148.         $name(Pname,Namelist),
  149.         Tcall =.. [Pname|Args],$write(Tcall),
  150.         $deb_prompt(Of,Type,Spy,C).
  151.  
  152. $deb_writepref(spy) :- $writename('**').
  153. $deb_writepref(nospy) :- $writename('  ').
  154.  
  155. $deb_prompt(Of,'Call',nospy,C) :-
  156.     $deb_getonechar(C),$tell(Of),$deb_ug(C).
  157.  
  158. $deb_prompt(Of,'Fail',nospy,C) :-
  159.     /*nl,$tell(Of).*/
  160.     $deb_getonechar(C),$tell(Of),$deb_ug(C).
  161.  
  162. $deb_prompt(Of,'Exit',nospy,C) :-
  163.     $nl,$tell(Of).
  164.  
  165. $deb_prompt(Of,_,spy,C) :-
  166.     $deb_getonechar(C),$tell(Of),$deb_ug(C).
  167.  
  168. $deb_ug(10) :- !,    /* <cr>: creep */
  169.     $globalset($deb_tracing(1)).
  170. $deb_ug(97) :- !,abort.    /* a: abort */
  171. $deb_ug(98) :- !,    /* break */
  172.     break.    /* and creep when return */
  173. $deb_ug(99) :- !,    /* c: creep */
  174.     $globalset($deb_tracing(1)).
  175. $deb_ug(101) :- !,halt.    /* e: exit Prolog */
  176. $deb_ug(102) :- !,fail.    /* f: fail */
  177. $deb_ug(108) :- !,    /* l: leap */
  178.     $globalset($deb_tracing(0)).
  179. $deb_ug(110) :- !,    /* n: nodebug */
  180.     $nodebug.
  181. $deb_ug(113) :- !,    /* q: quasi-skip */
  182.     $globalset($deb_quasiskip(1)).
  183. $deb_ug(114) :- !.    /* r: retry */
  184. $deb_ug(115) :- !,    /* s: skip */
  185.     $globalset($deb_quasiskip(1)),
  186.     $globalset($deb_skipping(1)).
  187.  
  188. $deb_printhelp :- $telling(Of),$tell(user),
  189.     $tab(3),$writename('<cr>'),$tab(3),$writename('creep'),
  190.     $tab(10),$writename('a'),$tab(6),$writename('abort'),$nl,
  191.     $tab(3),$writename('c'),$tab(6),$writename('creep'),
  192.     $tab(10),$writename('f'),$tab(6),$writename('fail'),$nl,
  193.     $tab(3),$writename('r'),$tab(6),$writename('retry (fail)'),
  194.     $tab(3),$writename('h'),$tab(6),$writename('help'),$nl,
  195.     $tab(3),$writename('n'),$tab(6),$writename('nodebug'),
  196.     $tab(8),$writename('e'),$tab(6),$writename('exit'),$nl,
  197.     $tab(3),$writename('b'),$tab(6),$writename('break'),
  198.     $tab(10),$writename('s'),$tab(6),$writename('skip'),$nl,
  199.     $tab(3),$writename('q'),$tab(6),$writename('quasi-skip'),
  200.     $tab(5),$writename('l'),$tab(6),$writename('leap'),
  201.     $tell(Of).
  202.  
  203. $deb_getonechar(C) :- 
  204.     $writename(' ? '),
  205.     $seeing(If),$see(user),$get0(C1),
  206.     (C1=:=10,C=C1,$see(If)
  207.     ;
  208.      C1=\=10,$deb_skiptoaft,
  209.         (C1=:=104, /*help*/
  210.         $deb_printhelp,$deb_getonechar(C)
  211.         ;
  212.          C1=\=104,C=C1,$see(If)
  213.         )
  214.     ).
  215.     
  216. $deb_skiptoaft :- $get0(C),C=\=10,!,$deb_skiptoaft.
  217. $deb_skiptoaft.
  218.  
  219. $spy(X) :- $untrace(X),$deb_setspy(X),$debug,$globalset($deb_tracing(0)).  
  220.  
  221. $deb_setspy(X) :-
  222.     (nonvar(X), $deb_setspy0(X)) ;
  223.     (var(X), print('*** spy: argument cannot be a variable ***'), nl).
  224.  
  225. $deb_setspy0(P/A) :- 
  226.     (atomic(P), integer(A)) ->
  227.         ( ($symtype('_$spy_points'(_),Type),
  228.            Type > 0,
  229.            not('_$spy_points'(P/A)),!,    /* noop if already there */
  230.            $assert('_$spy_points'(P/A)),
  231.            $deb_set(P,A,$deb_spy(_))
  232.           ) ;
  233.           true
  234.         ) ;
  235.         (print('*** illegal arguments to spy: '), print(P), print('/'), print(A), $nl).
  236. $deb_setspy0([Pred|More]) :- $deb_setspy0(Pred),$deb_setspy(More).
  237. $deb_setspy0([]).
  238.  
  239. $trace(X) :- $debug,$deb_settrace(X).
  240.  
  241. $deb_settrace(X) :-
  242.     (nonvar(X), $deb_settrace0(X)) ;
  243.     (var(X), print('*** trace: argument cannot be a variable ***'), nl).
  244.  
  245. $deb_settrace0(P/A) :-
  246.     (atomic(P), integer(A)) ->
  247.         (($symtype('_$traced_preds'(_),Type),
  248.            Type > 0,
  249.           '_$traced_preds'(P/A)
  250.           ) ->
  251.             (print('*** trace: already tracing '), print(P), print('/'), print(A),
  252.              nl
  253.             ) ;
  254.             ($functor(F,P,A),
  255.              $symtype(F,Symtype),
  256.              ((Symtype =:= 1 ; Symtype =:= 2) ->
  257.                 ($assert('_$traced_preds'(P/A)),
  258.                   $deb_set(P,A,$deb_trace(_))
  259.                 ) ;
  260.                 (print('*** '), print(P), print('/'), print(A),
  261.                  print(' not defined, cannot trace ***'), $nl
  262.                 )
  263.              )
  264.             )
  265.         ) ;
  266.         (print('*** illegal argument to trace: '),print(X), nl).
  267. $deb_settrace0([Pred|More]) :- $deb_settrace0(Pred),$deb_settrace(More).
  268. $deb_settrace0([]).
  269.  
  270. /* add a check so that give error if try to trace undefined pred */
  271. $deb_set(P,A,Tracerout) :- 
  272.     $name(P,Pnamelist),
  273.     $name(Codepname,[99,111,100,101,36|Pnamelist]), /* code$p */
  274.     $functor(Codecall,Codepname,A),
  275.     $functor(Pcall,P,A),
  276.       /* make PRED and code$PRED have identical ep's */
  277.     $buff_code(Codecall,0,19 /* copy ep */ ,Pcall),
  278.       /* now define PRED(A1,..,An) :- $deb_trace(code$PRED(A1,..,An)). */
  279.     $defint_call(Pcall,A,Codecall,Tracerout).
  280.  
  281. $nospy(X) :- $deb_nospy(X),$trace(X),$nodebug.
  282.  
  283. $deb_nospy(X) :-
  284.     (nonvar(X), $deb_nospy0(X)) ;
  285.     (var(X), print('*** nospy: argument cannot be a variable ***'), nl).
  286.  
  287. $deb_nospy0(P/A) :- 
  288.     (atomic(P), integer(A)) ->
  289.         ( ($symtype('_$spy_points'(_),Type),
  290.            Type > 0,
  291.            '_$spy_points'(P/A),    /* no-op if not there */
  292.            $retract('_$spy_points'(P/A)),!,
  293.            $deb_unset(P/A)
  294.           ) ;
  295.           true
  296.         ) ;
  297.         (print('*** illegal argument to nospy: '), print(P), print('/'), print(A), nl).
  298. $deb_nospy0([Pred|More]) :- $deb_nospy0(Pred),$deb_nospy(More).
  299. $deb_nospy0([]).
  300.  
  301. $untrace(X) :- $deb_unsettrace(X).
  302.  
  303. $deb_unsettrace(X) :-
  304.     (nonvar(X), $deb_unsettrace0(X)) ;
  305.     (var(X), print('*** untrace: argument cannot be a variable ***'), nl).
  306.  
  307. $deb_unsettrace0(P/A) :-
  308.     (atomic(P), integer(A)) ->
  309.         ( ($symtype('_$traced_preds'(_),Type),
  310.            Type > 0,
  311.            '_$traced_preds'(P/A),
  312.            $retract('_$traced_preds'(P/A)),!,
  313.            $deb_unset(P/A)
  314.           ) ;
  315.           true        /* succeed even if retract fails */
  316.         ) ;
  317.         (print('*** illegal argument to untrace: '), print(P), print('/'), print(A), nl).
  318. $deb_unsettrace0([Pred|More]) :- $deb_unsettrace0(Pred),$deb_unsettrace(More).
  319. $deb_unsettrace0([]).
  320.  
  321. $deb_unset(P/A) :- /* add checks */
  322.     $name(P,Pnamelist),
  323.     $name(Codepname,[99,111,100,101,36|Pnamelist]), /* code$p */
  324.     $functor(Codecall,Codepname,A),
  325.     $functor(Pcall,P,A),
  326.     $buff_code(Pcall,0,19 /* copy ep */ ,Codecall).
  327.  
  328. $trace :- $flags(1,1). /* Simulator generated trace */
  329. $untrace :- $flags(1,0).
  330.  
  331. $debugging :- not( $debugging_0 ).
  332.  
  333. $debugging_0 :-
  334.     $deb_ugging(X),
  335.     ((X =:= 1, print('Debug mode on')) ; (X =\= 1, print('Debug mode off'))), nl,
  336.     $deb_tracepreds(L0),
  337.     (L0 = [] -> ( print('No predicates being traced'), nl) ;
  338.            (print('Traced predicates: '), $nl, tab(5), $deb_printtrace(L0))
  339.     ),
  340.     $deb_spypreds(L1),
  341.     (L1 = [] -> (print('No spy points set'), nl) ;
  342.            (print('Spy points set on: '), $nl, tab(5), $deb_printtrace(L1))
  343.     ),
  344.     !,
  345.     fail.
  346.  
  347. $deb_tracepreds(L) :-     $findall(X,'_$traced_preds'(X), L).
  348. $deb_spypreds(L) :-     $findall(X, '_$spy_points'(X), L).
  349.  
  350. $deb_printtrace([]) :- $nl.
  351. $deb_printtrace([(P/N)|L]) :- print(P), print('/'), print(N), print(', '), $deb_printtrace(L).
  352.  
  353. /* ------------------------------ $deb.P ------------------------------ */
  354.